package com.zhongketech.sunflower.activity.me;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.activity.BaseActivity;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.model.ImageUrl;
import com.zhongketech.sunflower.transport.model.User;
import com.zhongketech.sunflower.transport.uitl.NetTransportUtil;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class ModifyNameActivity extends BaseActivity implements OnClickListener {

	protected static final String TAG = "ModifyNameActivity";
	private EditText nameEditText;
	private 		Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_name);
		handler = new Handler() {
			public void handleMessage(Message msg) {
				finish();
			}
		};
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	//	private void setCallback() {
	//		mUpdateMeCallback = new UpdateMeCallback();
	//		mUpdateMeCallback.setListener(new UpdateMeListener() {
	//			
	//			@Override
	//			public void sucess(UserResponse arg0, Response arg1) {
	//				Log.d(TAG , "update name sucess------>");
	//				InputMethodManager imm = (InputMethodManager) ModifyNameActivity.this
	//						.getSystemService(Context.INPUT_METHOD_SERVICE);
	//				imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0);
	//				
	//				SunflowerApplication.setmUser(arg0.getUser());
	//				finish();
	//			}
	//			
	//			@Override
	//			public void failure(RetrofitError error) {
	//				Log.d(TAG , "update name fail------>");
	//				if (error.getResponse() != null) {
	//					int statusCode = error.getResponse().getStatus();
	//					if (statusCode == 500 || statusCode == 501) {
	//						final RestError body = (RestError) error.getBodyAs(RestError.class);
	//						Toast.makeText(ModifyNameActivity.this, body.errorDetails, Toast.LENGTH_SHORT).show();
	//					} else if (statusCode == 408) {
	//						Toast.makeText(ModifyNameActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
	//					} else {
	//						Toast.makeText(ModifyNameActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
	//					}
	//				}
	//			}
	//		});
	//	}

	private void initView() {
		//		TextView back = (TextView) findViewById(R.id.back);
		((TextView) findViewById(R.id.title)).setText("修改姓名");
		TextView other = (TextView) findViewById(R.id.other);
		nameEditText = (EditText) findViewById(R.id.name_edit);

		String name = getIntent().getExtras().getString("userName");
		nameEditText.setText(name);
		nameEditText.setSelection(name.length());// 设置光标位置到文字右边

		other.setText(getResources().getString(R.string.save));
		other.setVisibility(View.VISIBLE);
		other.setOnClickListener(this);
	}

	@Override
	public void back(View v)
	{
		InputMethodManager imm = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.other:
			updateUserInfo();
			break;
		default:
			break;
		}

	}

	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
	}

	protected void updateUserInfo() {
		final ProgressDialog pd = new ProgressDialog(this);
		pd.setCanceledOnTouchOutside(false);
		pd.setMessage("正在修改...");
		pd.show();

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();

				User tmpUser = MyApplication.getInstance().getUser();
				tmpUser.setName(nameEditText.getText().toString().trim());
				User newUser = new Action().ModifyMe(tmpUser);
				if(null != newUser)
				{
					final Message msg = new Message();
					handler.sendMessage(msg);
					newUser.setPassword(tmpUser.getPassword());
					MyApplication.getInstance().SetUser(newUser);
					MyToast.getInstance().show("更新成功");
				}
			}
		}).start();
	}
}
